

*Assign the directory for the table + figure output
global tnf .\TablesAndFigures\


********************************************************************************
* Table 1 - Summary of the credit bureau/HMDA match
********************************************************************************
use Data_CBMortgages, clear  // synthetic data created in _0_GenerateSyntheticData.do

**************************************
* Table 1 Panel A: Match Rates 
**************************************
tab matched if ex_loan_homepurchase==1
tab matched if ex_loan_homepurchase==0
tab matched

**************************************
* Table 1 Panel B/C: Summary stats for each sample
**************************************
* The code below writes output that can easily be pasted into a LaTex table.  

local varlist ex_loan_conventional ex_loan_fha ex_loan_va ex_loan_fannie ex_loan_freddie ex_loan_amount_exexact ///
              l1_vantage_v3_score age l1_d_havedebt_mta l1_all_balance_open l1_all_pastdue2 l1_aua_balance_open 

*note: for Panel B set homepurchase==1, for Panel C set homepurchase==0
local ifcondition0 "if ex_loan_homepurchase==1"
local ifcondition1 "if ex_loan_homepurchase==1 & matched==1"
local ifcondition2 "if ex_loan_homepurchase==1 & matched==0"

local clust ,vce(cluster countyyearid)

local n: word count `varlist'
forvalues i = 1/`n' {
    local var : word `i' of `varlist'
	
	*mean0
    qui summ `var' `ifcondition0', detail
	local mean0 `r(mean)'
	local var0 `r(Var)'	
	*mean1
    qui summ `var' `ifcondition1', detail
	local mean1 `r(mean)'
	local var1 `r(Var)'
	*mean2
	qui summ `var' `ifcondition2', detail
	local mean2 `r(mean)'
	local var2 `r(Var)'
	*normdiff
	local normdiff `= (`mean1' - `mean2') / sqrt(`var1' + `var2') '
	*t-stat of diff in means
	qui {
	capture drop zzzzz_ind1 zzzzz_insample
	gen zzzzz_insample=0
	replace zzzzz_insample=1 `ifcondition1'
	replace zzzzz_insample=1 `ifcondition2'
	gen zzzzz_ind1=0
	replace zzzzz_ind1=1 `ifcondition1'
	reg `var' zzzzz_ind1 if zzzzz_insample==1 `clust'
	mat b = e(b)
	mat v = e(V)
	local b = b[1,1]
	local se =sqrt(v[1,1])
	local tstat: di %9.2f `=`b'/`se''
	drop zzzzz_ind1 zzzzz_insample
		}
	if `i'==1 {
		di "" _col(30) " & mean(g0)" _col(40) " & mean(g1)" _col(50) " & mean(g2)" _col(60) " & Norm. Diff" _col(70) " & t-stat   \\"
		}
	di " `:var label `var''" _col(30) " & `:di %12.3g `mean0''" _col(40) " & `:di %12.3g `mean1''" _col(50) " & `:di %12.3g `mean2''" _col(60)  " & `:di %9.2f `normdiff''" _col(70) " & `:di %9.2f `tstat'' \\"
	}	
 
 

 
********************************************************************************
* Table 2 - Does borrower race affect the credit bureau/HMDA match? 
********************************************************************************

use Data_HmdaMortgages, clear // actual HMDA data - see Table 2 for a description

estimates clear	
*full sample
eststo: reghdfe matched loan_fha loan_va loan_fannie loan_freddie ln_loan_amount race_black race_hispanic black_ln_hmda_app_income hisp_ln_hmda_app_income ln_hmda_app_income ///
           , absorb(tractid#year) cluster(tractid#year)	
*home purchase mortgages
eststo: reghdfe matched loan_fha loan_va loan_fannie loan_freddie ln_loan_amount race_black race_hispanic black_ln_hmda_app_income hisp_ln_hmda_app_income ln_hmda_app_income ///
           if loan_homepurchase==1, absorb(tractid#year) cluster(tractid#year)
*refis
eststo: reghdfe matched loan_fha loan_va loan_fannie loan_freddie ln_loan_amount race_black race_hispanic black_ln_hmda_app_income hisp_ln_hmda_app_income ln_hmda_app_income ///
           if loan_homepurchase==0, absorb(tractid#year) cluster(tractid#year)	
		   
esttab _all using ${tnf}T_Match_HMDAReg  ,title("") ///
	replace tex frag ///
	noomitted  b(%12.3f) se(%12.3f) transform(100*@ 100) ///
	stats(r2 N, fmt(%12.3f %12.0gc) labels("R-Squared" "Observations")) se star(* .10 ** .05 *** .01) ///
	label nogaps nomtitles  ///
	mgroups("Full Sample" "Home Purchase Mortgages" "Refinance Loans", pattern(0 1 1)  span prefix(\multicolumn{@span}{c}{) suffix(}) erepeat(\cmidrule(lr){@span})) 	
	
	
	

	
	 